package server.messageContent;

import common.SerializationManager;
import common.ServiceResponseEnum;
import common.data.ResponseTypeEnum;
import common.interfaces.MessageContent;

public class CloseReplyMsgContent implements MessageContent{
	private byte[] data;
	private int length;
	
	public CloseReplyMsgContent(ServiceResponseEnum response, long accountId){
		//response type
		ResponseTypeEnum responseTypeEnum;
		if (response == ServiceResponseEnum.CLOSE_SUCCESSFUL){
			responseTypeEnum = ResponseTypeEnum.SUCCESSFUL;
		}
		else {
			responseTypeEnum = ResponseTypeEnum.ERROR;
		}
		
		byte[] responseTypeEnumStream = SerializationManager.getCurrent().fromResponseTypeEnum(responseTypeEnum);
		
		//response message
		String responseString = response.getMsg() + " " + String.valueOf(accountId);
		
		byte[] responseStream = SerializationManager.getCurrent()
				.fromString(responseString);
		byte[] responseStreamLength = SerializationManager.getCurrent()
				.fromInt(responseStream.length);

		//create message
		if (responseTypeEnum == ResponseTypeEnum.ERROR){
			data = new byte[responseTypeEnumStream.length
			                + responseStreamLength.length 
			                + responseStream.length];
			int currentIndex = 0;
			
			System.arraycopy(responseTypeEnumStream, 0, data, currentIndex, responseTypeEnumStream.length);
			currentIndex += responseTypeEnumStream.length;
			
			System.arraycopy(responseStreamLength, 0, data, currentIndex, responseStreamLength.length);
			currentIndex += responseStreamLength.length;
			
			System.arraycopy(responseStream, 0, data, currentIndex, responseStream.length);
		}
		else { //response object
			byte[] responseObjectStream = SerializationManager.getCurrent().fromBoolean(true);
			byte[] responseObjectLengthStream = SerializationManager.getCurrent().fromInt(responseObjectStream.length);
			
			data = new byte[responseTypeEnumStream.length
			                + responseStreamLength.length 
			                + responseStream.length
			                + responseObjectLengthStream.length
			                + responseObjectStream.length];
			int currentIndex = 0;
			
			System.arraycopy(responseTypeEnumStream, 0, data, currentIndex, responseTypeEnumStream.length);
			currentIndex += responseTypeEnumStream.length;
			
			System.arraycopy(responseStreamLength, 0, data, currentIndex, responseStreamLength.length);
			currentIndex += responseStreamLength.length;
			
			System.arraycopy(responseStream, 0, data, currentIndex, responseStream.length);
			currentIndex += responseStream.length;
			
			System.arraycopy(responseObjectLengthStream, 0, data, currentIndex, responseObjectLengthStream.length);
			currentIndex += responseObjectLengthStream.length;
			
			System.arraycopy(responseObjectStream, 0, data, currentIndex, responseObjectStream.length);
			currentIndex += responseObjectStream.length;
		}
		length = data.length;			
	}
	
	@Override
	public byte[] getByte() {
		return this.data;
	}

	@Override
	public int getLength() {
		return this.length;
	}

}
